package com.apexsoft.front.common.plugins.loader.impl;

import com.apexsoft.front.common.config.CommonConfig;
import com.apexsoft.front.common.plugins.loader.ILoader;
import com.apexsoft.front.common.plugins.loader.annotation.ALoader;
import com.apexsoft.front.common.utils.ClassesUtil;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.common.MonitorConstant;
import com.apexsoft.monitor.front.console.magent.service.MonitorThread;
import com.apexsoft.monitor.mq.LogMqDataConverter;
import com.apexsoft.monitor.mq.LogMqSubscribe;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletContext;
import java.util.List;


@ALoader(name = "monitor console init loader")
public class MonitorLoader implements ILoader {

    private static final Logger log = LogManager.getRootLogger();

    @Override
    public void load(ServletContext sc) {

            this.loadEditReqMappingByMethodAnnotation();

            MonitorConstant.MQ_HOST = CommonConfig.getString("MQ.HOST");
            MonitorConstant.MQ_PORT = CommonConfig.getInt("MQ.PORT");
            MonitorConstant.MQ_USERNAME = CommonConfig.getString("MQ.USERNAME");
            MonitorConstant.MQ_PASSWORD = CommonConfig.getString("MQ.PASSWORD");
            LogMqSubscribe.subscribe();

            new Thread(new LogMqDataConverter()).start();

            //监控服务自动启动
            new MonitorThread().start();
    }

    /**
     * 通过方法注解加载编辑方法的reqMapping
     */
    private void loadEditReqMappingByMethodAnnotation(){

        List<String> crudMappingList = ClassesUtil.getEditReqMappingByMethodAnnotation("com.apexsoft.esb.front.console", CrudMethod.class);
        MonitorConstant.crudReqMappingList = crudMappingList;
    }
}
